package com.bzm.config.im;

import java.time.LocalDateTime;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.bzm.common.utils.SerialNumberUtils;
import com.bzm.pojo.plugins.im.ImChatMsg;

import io.netty.channel.Channel;

/**
 * @Description:bzm im 聊天工具类
 * @author hemiao
 * @time:2022年9月30日 下午3:11:58
 */
public class BzmImChatUtils {
	
	/**
	 * @Description:消息验证
	 * @param msgObjJson
	 * @return
	 * ImChatMsg
	 * @exception:
	 * @author: hemiao
	 * @time:2022年9月30日 下午3:14:35
	 */
	public static ImChatMsg beforeMsgAuth(String msgObjJson)throws Exception {
		if(StringUtils.isBlank(msgObjJson)) {
			throw new RuntimeException("im消息不能为空");
		}
		ImChatMsg imChatMsg = null;
		try {
			imChatMsg = JSON.parseObject(msgObjJson, ImChatMsg.class);	
		} catch (Exception e) {
			throw new RuntimeException("im消息解析异常  e :"+e.getMessage());
		}
		if(imChatMsg==null) {
			throw new RuntimeException("im消息解析错误");
		}
		Integer msgType = imChatMsg.getMsgType();
		if(msgType==null) {
			throw new RuntimeException("消息类型不能为空");
		}	
		String msgContent = imChatMsg.getMsgContent();
		if(StringUtils.isNotBlank(msgContent)) {
			Integer max = 20;
			if(msgContent.length()>max) {	
				msgContent = msgContent.substring(0, max);
				msgContent += "【文本超出长度..."+max+"】";
			}	
			imChatMsg.setMsgContent(msgContent);
		}
		String sendId = imChatMsg.getSendId();
		// 发送时间	
		imChatMsg.setSendTime(LocalDateTime.now());
		// 消息id
		String msgId = SerialNumberUtils.createNumber(SerialNumberUtils.BZM_CODE, sendId);
		imChatMsg.setMsgId(msgId);
		return imChatMsg;
	}
	
	public static void main(String[] args) {
		
		String str = "123456789";
		System.out.println(str.substring(0, 3));
		
	}
}
